<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { login } from '@/api/user'

const router = useRouter()

// 表单数据
const email = ref('')
const password = ref('')
const loading = ref(false)

// 密码可见性状态
const passwordVisible = ref(false)

// 切换密码可见性
const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value
}

// 处理登录
const handleLogin = async () => {
  if (!email.value || !password.value) {
    message.warning('请填写邮箱和密码')
    return
  }

  // 登录前清除可能存在的旧 token
  sessionStorage.removeItem('token')
  sessionStorage.removeItem('userRole')
  sessionStorage.removeItem('userId')
  sessionStorage.removeItem('userName')
  sessionStorage.removeItem('userEmail')

  loading.value = true
  try {
    const response = await login(email.value, password.value)
    
    // 保存 token 和用户信息
    if (response.token) {
      sessionStorage.setItem('token', response.token)
    }
    
    if (response.user) {
      sessionStorage.setItem('userRole', response.user.role)
      sessionStorage.setItem('userId', response.user.id)
      sessionStorage.setItem('userName', response.user.name)
      sessionStorage.setItem('userEmail', response.user.email)
    }

    message.success('登录成功')
    
    // 根据角色跳转到默认页面
    const userRole = response.user?.role
    if (userRole === '人事专员') {
      router.push('/archive/register')
    } else if (userRole === '复核专员') {
      router.push('/archive/review')
    } else {
      router.push('/archive/query')
    }
  } catch (error) {
    // 错误处理已在 request.js 中完成
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

</script>

<template>
  <div class="apple-screen">
    <div class="aurora-bg">
      <div class="glow glow-1"></div>
      <div class="glow glow-2"></div>
      <div class="glow glow-3"></div>
    </div>

    <main class="glass-card">
      <div class="visual-panel">
        <div class="content-wrapper">
          <div class="logo-area">
            <svg
              width="40"
              height="40"
              viewBox="0 0 40 40"
              fill="none"
              xmlns="http://www.w3.org/2000/svg"
            >
              <rect width="40" height="40" rx="12" fill="white" fill-opacity="0.9" />
              <path d="M20 10L20 30" stroke="#007AFF" stroke-width="3" stroke-linecap="round" />
              <path d="M10 20L30 20" stroke="#007AFF" stroke-width="3" stroke-linecap="round" />
            </svg>
            <span class="app-name">HR Portal</span>
          </div>

          <div class="text-group">
            <h1>Hello.</h1>
            <p>欢迎回到工作空间</p>
          </div>
        </div>

        <div class="shimmer"></div>
      </div>

      <div class="form-panel">
        <form @submit.prevent>
          <div class="input-container">
            <label>邮箱</label>
            <div class="ios-input-wrapper">
              <input
                v-model="email"
                type="email"
                placeholder="name@company.com"
                class="ios-input"
                required
                :disabled="loading"
              />
            </div>
          </div>

          <div class="input-container">
            <label>密码</label>
            <div class="ios-input-wrapper">
              <input
                v-model="password"
                :type="passwordVisible ? 'text' : 'password'"
                placeholder="填写密码"
                class="ios-input"
                required
                :disabled="loading"
                @keyup.enter="handleLogin"
              />
              <div class="toggle-eye" @click="togglePasswordVisibility">
                <svg
                  v-if="!passwordVisible"
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                >
                  <path
                    d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"
                  ></path>
                  <line x1="1" y1="1" x2="23" y2="23"></line>
                </svg>
                <svg
                  v-else
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                >
                  <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                  <circle cx="12" cy="12" r="3"></circle>
                </svg>
              </div>
            </div>
            <div class="help-links">
              <a href="#">忘记密码?</a>
            </div>
          </div>

          <button
            type="submit"
            class="primary-btn"
            :disabled="loading"
            @click="handleLogin"
          >
            <span class="btn-text">{{ loading ? '登录中...' : '登 录' }}</span>
            <svg
              v-if="!loading"
              class="arrow-icon"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2.5"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <line x1="5" y1="12" x2="19" y2="12"></line>
              <polyline points="12 5 19 12 12 19"></polyline>
            </svg>
          </button>
        </form>
      </div>
    </main>
  </div>
</template>

<style scoped>
/* ----------------------------------
   Design Tokens: macOS Titanium
   ---------------------------------- */
:root {
  --primary-blue: #0071e3; /* Apple Official Blue */
  --primary-blue-hover: #0077ed;
  --bg-input: #f5f5f7;
  --text-main: #1d1d1f;
  --text-sub: #86868b;
  --focus-glow: rgba(0, 113, 227, 0.25);
  --card-radius: 32px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* 沉浸式屏幕 */
.apple-screen {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  overflow: hidden;
  font-family:
    -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text', Helvetica, Arial, sans-serif;
}

/* 1. 背景光效 (更高级、更柔和) */
.aurora-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  opacity: 0.8;
}

.glow {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  animation: float 15s infinite ease-in-out alternate;
}
/* 蓝色光晕 */
.glow-1 {
  top: -10%;
  left: -10%;
  width: 60vw;
  height: 60vw;
  background: radial-gradient(circle, #e0f2ff 0%, rgba(255, 255, 255, 0) 70%);
}
/* 紫粉色光晕 (点缀) */
.glow-2 {
  bottom: -10%;
  right: -10%;
  width: 50vw;
  height: 50vw;
  background: radial-gradient(circle, #f4f0ff 0%, rgba(255, 255, 255, 0) 70%);
  animation-delay: -5s;
}
/* 中间连接色 */
.glow-3 {
  top: 40%;
  left: 40%;
  width: 30vw;
  height: 30vw;
  background: #fbfbfd;
  animation-delay: -10s;
}

@keyframes float {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(20px, 40px);
  }
}

/* 2. 磨砂卡片 */
.glass-card {
  position: relative;
  z-index: 10;
  display: flex;
  width: 860px;
  height: 520px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(50px) saturate(150%); /* 极致毛玻璃 */
  -webkit-backdrop-filter: blur(50px) saturate(150%);
  border-radius: var(--card-radius);
  border: 1px solid rgba(255, 255, 255, 0.8);
  box-shadow:
    0 20px 40px -10px rgba(0, 0, 0, 0.08),
    0 0 0 1px rgba(255, 255, 255, 0.5) inset;
  overflow: hidden;
}

/* 左侧视觉 */
.visual-panel {
  flex: 1;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, rgba(230, 240, 255, 0.4) 100%);
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 60px;
}
.app-name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-main);
  opacity: 0.8;
  letter-spacing: 0.5px;
}

.text-group h1 {
  font-size: 3.5rem;
  font-weight: 700;
  color: var(--text-main);
  letter-spacing: -1.5px;
  line-height: 1;
  margin-bottom: 12px;
  background: linear-gradient(135deg, #1d1d1f 0%, #434344 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.text-group p {
  font-size: 1.1rem;
  color: var(--text-sub);
  font-weight: 400;
}

/* 装饰光效 */
.shimmer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    45deg,
    transparent 40%,
    rgba(255, 255, 255, 0.6) 50%,
    transparent 60%
  );
  pointer-events: none;
}

/* 右侧表单 */
.form-panel {
  flex: 1;
  background: #ffffff;
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* 输入框组 */
.input-container {
  margin-bottom: 24px;
}

.input-container label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-sub);
  margin-bottom: 8px;
  margin-left: 4px;
}

.ios-input-wrapper {
  position: relative;
  background: var(--bg-input);
  border-radius: 14px; /* iOS 风格圆角 */
  transition: all 0.3s cubic-bezier(0.25, 0.1, 0.25, 1);
  border: 1px solid transparent;
}

/* 聚焦动效：背景变白，光晕浮现 */
.ios-input-wrapper:focus-within {
  background: #fff;
  border-color: var(--primary-blue);
  box-shadow: 0 0 0 4px var(--focus-glow);
  transform: translateY(-1px);
}

.ios-input {
  width: 100%;
  padding: 16px;
  font-size: 16px;
  border: none;
  background: transparent;
  color: var(--text-main);
  outline: none;
  font-weight: 400;
}
.ios-input::placeholder {
  color: #bfbfc4;
}

/* 眼睛图标 */
.toggle-eye {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #98989d;
  cursor: pointer;
  display: flex;
  transition: color 0.2s;
}
.toggle-eye:hover {
  color: var(--primary-blue);
}
.toggle-eye svg {
  width: 20px;
  height: 20px;
}

.help-links {
  margin-top: 8px;
  text-align: right;
}
.help-links a {
  font-size: 12px;
  color: var(--text-sub);
  text-decoration: none;
  transition: color 0.2s;
}
.help-links a:hover {
  color: var(--primary-blue);
  text-decoration: underline;
}

/* 主按钮 */
.primary-btn {
  margin-top: 16px;
  width: 100%;
  height: 52px;
  background: var(--primary-blue);
  color: black;
  border: none;
  border-radius: 99px; /* 胶囊形 */
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); /* 弹性动效 */
  box-shadow: 0 4px 12px rgba(0, 113, 227, 0.2);
}

.primary-btn:hover {
  background: var(--primary-blue-hover);
  transform: scale(1.02);
  box-shadow: 0 6px 20px rgba(0, 113, 227, 0.3);
}

.primary-btn:active {
  transform: scale(0.96);
}

.arrow-icon {
  width: 18px;
  height: 18px;
  transition: transform 0.2s;
}
.primary-btn:hover .arrow-icon {
  transform: translateX(3px);
}

/* 响应式 */
@media (max-width: 900px) {
  .glass-card {
    flex-direction: column;
    width: 90%;
    height: auto;
    max-width: 420px;
  }
  .visual-panel {
    padding: 40px;
    padding-bottom: 20px;
    align-items: center;
    text-align: center;
  }
  .logo-area {
    margin-bottom: 20px;
  }
  .text-group h1 {
    font-size: 2.5rem;
  }
  .form-panel {
    padding: 40px;
  }
}
</style>
